<template>
  <div class="login">
    <div class="formbox">
      <div class="title">
        <h1>vite</h1>
      </div>
      <div class="content">
        <div class="img">
123
        </div>
        <div class="form">
          <div class="form-title">
            <h1>vite管理系统</h1>
            <div class="form-content">
              <el-form ref="ruleFormRef" :model="ruleform" :rules="rules" hide-required-asterisk>
                <el-form-item label="账号" prop="username" label-width="100">
                  <el-input v-model.trim="ruleform.username" placeholder="请输入账号" class="input">
                    <template #prefix>
                      <el-icon class="el-input__icon">
                        <Avatar />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password" label-width="100">
                  <el-input v-model.trim="ruleform.password" placeholder="请输入密码" class="input">
                    <template #prefix>
                      <el-icon class="el-input__icon">
                        <Key />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code" label-width="100">
                  <el-input v-model.trim="ruleform.code" placeholder="请输入验证码" class="inputcode">
                    <template #prefix>
                      <el-icon class="el-input__icon">
                        <Iphone />
                      </el-icon>
                    </template>
                  </el-input>
                  <div class="inputcode" v-loading="loading">
                    <img :src="img" alt="" class="inputcode" v-if="img" @click="getCodeImgs">
                    <span v-else>你把验证码弄丢了..</span>
                  </div>
                </el-form-item>
                <el-form-item prop="rememberMe" label-width="100">
                  <el-checkbox v-model="ruleform.rememberMe" label="记住密码" size="large" />
                </el-form-item>
                <div>
                  <el-button type="primary" class="login-btn" @click="goadmin" v-if="!btnloading">登录</el-button>
                  <el-button type="primary" class="login-btn" v-else disabled loading>登录中</el-button>
                </div>


              </el-form>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script  setup>
import Cookies from "js-cookie";
import { getCodeImg } from '@/api/login'
import useUserStore from '@/stores/user'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute();
const router = useRouter();
const loading = ref(false)
const btnloading = ref(false)
const ruleFormRef=ref(null)
const ruleform = ref({
  username: 'admin',
  password: 'admin123',
  code: '',
  uuid:"",
  rememberMe: false
})
const redirect = ref(null);
const img = ref('')
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ], 
}
const goadmin = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      btnloading.value = true
      if (ruleform.value.rememberMe) {//记住用户名
        Cookies.set("username", ruleform.value.username, { expires: 30 });
        Cookies.set("password", encrypt(ruleform.value.password), { expires: 30 });
        Cookies.set("rememberMe", ruleform.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      useUserStore().Login(ruleform.value).then(() => {
        const query = route.query;
        const otheroarans=Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur];
          }
          return acc;
        }, {})
        if (redirect.value) {
          router.push({ path: redirect.value, query: otheroarans });
        } else {
          router.push({ path: "/", query: otheroarans });
        }
      }).catch(() => {
        getCodeImgs()
        btnloading.value = false
      });
    } else {
      console.log('error submit!')
      return false
    }
  })
}
const getCodeImgs = () => {
  loading.value = true
  getCodeImg().then((res) => {
    img.value = "data:image/png;base64," + res.img
    ruleform.value.uuid = res.uuid
  }).finally(() => {
    loading.value = false
  })
}
onMounted(() => {
  getCodeImgs()
})
watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
console.log(redirect.value,'redirect',newRoute.query,'query');

  
}, { immediate: true });
</script>

<style lang="scss" scoped>
.login{
     width: 100%; /* 视口宽度 */
    height: 100vh; /* 视口高度 */
    display: flex; /* 灵活布局 */
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
    background: url(../assets/img/bg.png) no-repeat center/cover;
    .formbox{
      display: flex;
      flex-direction: column;
      height: 400px;
      width: 800px;
      background-color: rgba(255, 255, 255, .5);
        border-radius: 5px;
        box-shadow: 0 0 8px 0 #eee;
        backdrop-filter: blur(2px);
        .title{
  height: 50px;
    font-weight: bold;
    background-image: linear-gradient(to right, #829BFF, #FEC726);
    -webkit-background-clip: text;
    color: transparent;
  text-align: center;
        }
        .content{
          display: flex;
          height: 100%;
          .img{
            width:30%;
            height: 100%;
            background-color: #fff;
            background: url('../assets/img/logo.png') no-repeat center/cover;
            background-size:100%;

          }
          .form{
         
flex: 1;
margin: auto;
text-align: center;
padding: 0 20px;
.form-title{
display: flex;
flex-direction: column;

  h1{
    opacity: 0.7;
  }
  .form-content{
    width: 100%;
    .input{
      width: 100%;
    
    }
    .inputcode{
      height: 32px;
      width: 50%;

    }
    .login-btn{
      width: 200px;
      margin-top: 10px;
    }
  }
}
          }
          

        }
    }

}

</style>